package com.skt.common_hr.congmoney_sac.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.congmoney_sac.CelebrationFacade;
	import com.skt.common_hr.congmoney_sac.model.vo.CelebrationCodeVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class CelebrationCodeProxy extends CommonProxy
	{
		public static const NAME:String = "CelebrationCodeProxy";

		public static const LOAD_HOLIDAYS_COMPLETE:String = "loadHolidayComplete";
		public static const LOAD_HOLIDAYS_FAILED:String = "loadHolidayFailed";
		//경조사유콤보
		public var codeList:ArrayCollection = null;
		public var holidays:Array = [];

		public function CelebrationCodeProxy(data:Object=null)
		{
			super(NAME, data);
		}

		//경조사유콤보
		public function loadCodes(pernr:String, corsn:String, sCodat:String=''):void
		{
			var param:Object = new Object();
			param.I_PERNR = pernr;
			param["I_COGBN"] = corsn;
			param["I_CODAT"] = sCodat;

			loadData("Z_HR_0044", param, corsnResult, corsnFault);
		}

		public function loadHolidays(beg:Date=null, end:Date=null):void
		{
			if(beg==null) {
				beg = DateUtil.todayNoTime();
				end = DateUtil.todayNoTime();
				end.month = beg.month = 0;
				end.date = beg.date = 1;
				end.fullYear += 1;
				end.date--;
			}
			if(end==null) {
				end = new Date(beg);
				end.fullYear += 1;
			}

			var param:Object = new Object();
			param.I_FROM = DateUtil.toBasicString(beg);
			param.I_TO = DateUtil.toBasicString(end);

			loadData("Z_HR_0112", param, holidayResult, holidayFault);
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- 경조사유콤보
		 * @param
		 * @return void
		 */
		public function corsnResult(event:ResultEvent):void
		{
			if(retCode==0) {
				codeList = parseCodes(event.result.FTAB);
				facade.sendNotification(CelebrationFacade.LOAD_CELEBRATION_CODE_COMPLETE, this);
			}
			else {
				facade.sendNotification(CelebrationFacade.LOAD_CELEBRATION_CODE_FAILED, this);
			}
		}

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- 경조사유콤보
		 * @param
		 * @return void
		 */
		public function holidayResult(event:ResultEvent):void
		{
			if(retCode==0) {
				holidays = parseHolidays(event.result.CALTAB);
				facade.sendNotification(LOAD_HOLIDAYS_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_HOLIDAYS_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 		- 경조사유콤보
		 * @param
		 * @return void
		 */
		private function corsnFault(event:FaultEvent):void
		{
			facade.sendNotification(CelebrationFacade.LOAD_CELEBRATION_CODE_FAILED, this);
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 		- 경조사유콤보
		 * @param
		 * @return void
		 */
		private function holidayFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_HOLIDAYS_FAILED, this);
		}


		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param ArrayCollection
		 * @return 경조사유 콤보 ArrayCollection
		 */
		private function parseCodes(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			var vo:CelebrationCodeVO = new CelebrationCodeVO;

			vo.ZTEXT = ResourceManager.getInstance().getString("CongMoney", "SELECT_REASON");
			retArr.addItem(vo);

			for each(var rec:Object in srcArr)
			{
				vo = new CelebrationCodeVO;
				vo.CORSN 	= rec.CORSN;
				vo.ZTEXT 	= rec.ZTEXT;
				vo.FAMILY 	= rec.FAMILY;
				vo.FTEXT 	= rec.FTEXT;
				vo.BETRG 	= rec.BETRG;
				vo.ZWRTH 	= rec.ZWRTH;
				vo.WAERS 	= rec.WAERS;
				vo.CODAY 	= rec.CODAY;
				vo.ZFNR		= rec.ZFNR;
				retArr.addItem(vo);
			}
			return retArr;
		}

		private function parseHolidays(srcArr:ArrayCollection):Array
		{
			var retArr:Array = new Array();
			for each(var rec:Object in srcArr)
			{
				var holiday:Date = DateUtil.stringToDateTime(rec.DATUM);
				if(holiday.day >0 && holiday.day <6) retArr.push(holiday);
			}
			return retArr;
		}
	}
}